package menu.lt.pers.menu;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.*;
import menu.lt.pers.menu.Bean.UserBean;
import menu.lt.pers.menu.dao.UserDao;

public class RegisterActivity extends FragmentActivity {
    private TextView tvTitle;
    private EditText etPhone;
    private EditText etPwd;
    private Button btnLogin;
    private Button btnRegister;
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();

    }

    private void init() {
        linearLayout = findViewById(R.id.login_linearlayout);
        tvTitle =  findViewById(R.id.textView);
        etPhone =  findViewById(R.id.et_phone);
        etPwd =  findViewById(R.id.et_pwd);
        btnLogin =  findViewById(R.id.btn_login);
        btnRegister =  findViewById(R.id.btn_register);
        tvTitle.setText("注册");

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserBean user = new UserBean();
                user.setPhone(etPhone.getText().toString());
                UserDao dao = new UserDao(RegisterActivity.this);
                Cursor cursor = dao.findByPhone(user);
                if (cursor.getCount()>0){
                    Toast.makeText(RegisterActivity.this,
                            getResources().getString(R.string.register_error_msg),
                            Toast.LENGTH_SHORT).show();
                } else {
                    user.setPwd(etPwd.getText().toString());
                    dao.insert(user);
                    Toast.makeText(RegisterActivity.this,
                            getResources().getString(R.string.register_success),
                            Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
    }
}
